import store from './../store'

var scrollHandle = (function () {
    var callbacks = [];

    window.addEventListener('scroll', function () {
        var timer,
            lastTime = Date.now(),
            interval = 150;

        var load = function () {
            if(document.body.scrollTop + 1.5 * window.innerHeight > document.documentElement.scrollHeight){
                callbacks.forEach(function (callback, index) {
                    callback.call();
                })
            }
        }
        return function () {

            var now = Date.now();
            if(timer){
                clearTimeout(timer);
            }

            if(now - lastTime >= interval){
                load()
                lastTime = now;
            }else{
                timer = setTimeout(load, interval);
            }

            store.lastScrollTop = document.body.scrollTop;
        }

    }());

    return function (func) {
        if(func){
            callbacks.push(func);
        }
    };
}())


export default scrollHandle



